import json
import logging
import requests

from mcp.server.fastmcp import FastMCP

import __init__

mcp = FastMCP("question_classification")


@mcp.tool('question_classification')
async def question_classification(message: str, category_list: list[dict]):
    # 模型地址
    classification_url = "http://110.122.0.37:32301/question_classifier"
    headers = {
        "Content-Type": "application/json",
        "Accept": "application/json"
    }
    # 构造数据
    for item in category_list:
        if "category_desc" in item:
            item["category_desc"] = item["category_desc"].encode("utf-8").decode("unicode_escape")
    payload = {
        "message": message.encode('utf-8').decode('unicode_escape'),
        "category_list": category_list
    }
    response = requests.post(classification_url, data=json.dumps(payload), headers=headers)
    return response.json()


# 该启动函数是必须的，不能删除
if __name__ == "__main__":
    # 日志打印是确保__init__引用
    logging.debug('', __init__)
    mcp.run(transport='stdio')
